<?php

 Class MemberFavorClassModel extends Model
 {

     /**
      * 获得单条数据
      * @param $condition 条件
      * @return mixed
      */
     public function getfclassByOne($condition, $field)
     {
         return $this->where($condition)->field($field)->find();
     }

     /**
      * 获得多条数据
      * @param $condition 条件
      * @param $field 字段
      * @param $order 排序
      * @param $limit 数量
      * @return mixed
      */
     public function getfclassByMore($condition, $field, $order, $limit)
     {
         return $this->where($condition)->field($field)->order($order)->limit($limit)->select();
     }

     /**
      * @param $data 数据
      * @return mixed|void
      */
     public function insert($data)
     {
         if (empty($data)) {
             return;
         }
         return $this->add($data);
     }

     /**
      * 更新数据
      */
     public function updata($condition, $data)
     {
         return $this->where($condition)->save($data);
     }

     /**
      *  获取分类列表
      */
     public function getFavorClassList($data){
           if(!$data['inAjax'])  return ['status'=>400,'msg'=>'分类获取失败'];
           $where['uid'] = session('uid');
           $where['type'] = 1;
           $favorClasslist = $this->getfclassByMore($where);
           if($favorClasslist){
                 return ['status'=>200,'msg'=>'ok','data'=>$favorClasslist];
           }
           return ['status'=>400,'msg'=>'分类获取失败'];
     }

     /**
      * 添加分类
      * @param $data 数据
      * @return array
      */
     public function addFavorClass($data){
         if(!$data['fid']){
             $res = ['status'=>400,'msg'=>'参数错误'];
             return $res;
         }
         if(!session('uid')){
             $res = ['status'=>400,'msg'=>'未登录状态'];
             return $res;
         }

         // 判断是否已有本分类
         $where['ftypename'] = $data['fid'];
         $where['type'] = 1;
         $where['uid'] = session('uid');
         $isHas = $this->getfclassByOne($where);
         if($isHas){
             $res = ['status'=>400,'msg'=>'该分类已存在'];
             return $res;
         }

         $insert['uid'] = session('uid');
         $insert['type'] = 1;
         $insert['ftypename'] = $data['fid'];

         $res = $this->insert($insert);

         if($res){
             $res = ['status'=>200,'msg'=>'分类添加成功'];
             return $res;
         }
         $res = ['status'=>400,'msg'=>'分类添加失败'];
         return $res;
     }
 }